package com.bbms.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bbms.dao.BorrowRecordDao;
import com.bbms.entity.BorrowRecord;
import com.bbms.entity.Page;
import com.bbms.service.BorrowRecordService;

@Service("BorrowRecordServiceImpl")
public class BorrowRecordServiceImpl implements BorrowRecordService{

	@Resource(name="BorrowRecordDaoImpl")
	private BorrowRecordDao brd;
	
	
	@Override
	public void add(BorrowRecord t) {
	}

	@Override
	public void update(BorrowRecord t) {
		
	}

	/**
	 * 归还图书操作
	 */
	@Override
	public void delete(BorrowRecord t) {
		
		brd.delete(t);
		brd.updateReader(t.getReaderId(), 0);
		brd.updateBook(t.getBookId(), 1);
	}

	@Override
	public Page getPage(int currentPage, int pageSize) {
		return null;
	}
	

	/*
	 * 获取当前管理员id
	 * @see com.bbms.service.BorrowRecordService#getCurrentManagerId(java.lang.String)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public int getCurrentManagerId(String account) {
		
		List<HashMap> list=brd.getCurrentManager(account);
		
		int managerId=Integer.parseInt(list.get(0).get("id").toString());
		
		return managerId;
	}

	/**
	 * 获取分页数据（含查询）
	 */
	@Override
	public Page getPage(int currentPage, int pageSize, String s_book, String s_reader, String s_manager,
			Integer s_renewBorrowNum, Integer s_status, String borrowStartTime, String borrowEndTime,
			String restoreStartTime, String restoreEndTime) {
		
		Page page=new Page();
		
		page=brd.getPage(currentPage, pageSize, s_book, s_reader, s_manager, s_renewBorrowNum, s_status, 
											borrowStartTime, borrowEndTime, restoreStartTime, restoreEndTime);
		return page;
	}

	/*
	 * 是否顺利借书
	 * @see com.bbms.service.BorrowRecordService#insert(com.bbms.entity.BorrowRecord)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public boolean insertRecord(BorrowRecord record) {
		
		List<HashMap> list=brd.findReaderInfoById(record.getReaderId());
		
		int bookNum=Integer.parseInt(list.get(0).get("bookNum").toString());		//当前借阅册数
		int maxBookNum=Integer.parseInt(list.get(0).get("maxBookNum").toString());  //最大借阅册数
		int borrowMaxTime=Integer.parseInt(list.get(0).get("maxTime").toString());		//可借阅时间
		
		//结算是否可借阅
		if (bookNum < maxBookNum) {
			
			brd.addBorrowRecord(record, borrowMaxTime);
			brd.updateReader(record.getReaderId(), 1);  	//1表示借阅
			brd.updateBook(record.getBookId(), 0); 			//0表示外借
			return true;
		}
		
		return false;
	}

	
	/*
	 * 是否修改记录成功
	 * @see com.bbms.service.BorrowRecordService#updateRecord(com.bbms.entity.BorrowRecord)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public boolean updateRecord(BorrowRecord record) {
		
		List<HashMap> list=brd.findRecordById(record.getId());
		int lastReaderId=Integer.parseInt(list.get(0).get("readerId").toString());		//上次记录读者ID
		int lastBookId=Integer.parseInt(list.get(0).get("bookId").toString());		//上次借阅图书
		
		brd.updateReader(lastReaderId, 0);	//0表示回滚
		brd.updateBook(lastBookId, 1);  	//1表示回滚成图书在库
		
		List<HashMap> list2=brd.findReaderInfoById(record.getReaderId());				//查找当前读者信息
		
		int bookNum=Integer.parseInt(list2.get(0).get("bookNum").toString());		//当前借阅册数
		int maxBookNum=Integer.parseInt(list2.get(0).get("maxBookNum").toString());  //最大借阅册数
		int borrowMaxTime=Integer.parseInt(list2.get(0).get("maxTime").toString());		//可借阅时间
		
		//判断该读者是否可借阅
		if (bookNum<maxBookNum) {
			
			brd.updateBorrowRecord(record, borrowMaxTime, 1);	//1表示修改
			brd.updateReader(record.getReaderId(), 1);			//1表示借阅
			brd.updateBook(record.getBookId(), 0);  			//0表示外借
			return true;
		}
		return false;
	}

	/*
	 * 是否成功续借
	 * @see com.bbms.service.BorrowRecordService#renewBorrow(com.bbms.entity.BorrowRecord)
	 */
	@SuppressWarnings({ "rawtypes" })
	@Override
	public boolean renewBorrow(BorrowRecord record) {
		
		int renewNum=record.getRenewBorrowNum();		//当前续借次数
		
		List<HashMap> list=brd.findReaderInfoById(record.getReaderId());				 	//查找当前读者信息
		int renewMaxNum=Integer.parseInt(list.get(0).get("renewMaxNum").toString()); 		//最大续借次数
		int renewMaxTime=Integer.parseInt(list.get(0).get("renewMaxTime").toString());		//可续借时间
		
		if (renewNum<renewMaxNum) {
			
			brd.updateBorrowRecord(record, renewMaxTime, 0);
			return true;
		}
		return false;
	}

	/*
	 * 批量归还是否成功
	 * @see com.bbms.service.BorrowRecordService#bacthDel(java.util.List)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public void bacthDel(List<Integer> list,int managerId) {
		
		BorrowRecord record=new BorrowRecord();
		
		for (int i = 0; i < list.size(); i++) {
			
			int recordId=Integer.parseInt(list.get(i).toString());			//记录ID
			
			List<HashMap> recordList=brd.findRecordById(recordId);			//一条记录
			int readerId=Integer.parseInt(recordList.get(0).get("readerId").toString());	//读者ID
			int bookId=Integer.parseInt(recordList.get(0).get("bookId").toString());		//图书ID

			record.setId(recordId);
			record.setBookId(bookId);
			record.setReaderId(readerId);
			record.setManagerId(managerId);
			
			brd.delete(record);
			brd.updateReader(record.getReaderId(), 0);
			brd.updateBook(record.getBookId(), 1);
			
		}
	
	}


}
